package com.citas.client.widget;

import java.util.ArrayList;
import java.util.Date;

import com.citas.client.CitasConstants;
import com.citas.client.CitasMessages;
import com.citas.client.Display;
import com.citas.client.Getdata;
import com.citas.client.GetdataAsync;
import com.citas.shared.Encriptar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.IntegerBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.user.client.ui.Image;

public class Registrarse extends Composite {

	private static RegistrarseUiBinder uiBinder = GWT
			.create(RegistrarseUiBinder.class);
	@UiField TextBox usuario;
	@UiField PasswordTextBox pass1;
	@UiField PasswordTextBox pass2;
	@UiField TextBox nombres;
	@UiField TextBox paterno;
	@UiField TextBox materno;
	@UiField ListBox anio;
	@UiField DateBox fecha;
	@UiField ListBox estadocivil;
	@UiField DecoratorPanel personales;
	@UiField Button button;
	@UiField ListBox sexo;
	@UiField Image image;
	@UiField TextBox direccion;
	@UiField TextBox dni;
	@UiField Button bot_registrarse;
	@UiField TextBox factor_rh;
	@UiField TextBox grupo_sanguineo;
	@UiField IntegerBox tb_telefono;
	CitasConstants cons = GWT.create(CitasConstants.class);
	CitasMessages msn= GWT.create(CitasMessages.class);
	
	Command comando=null;
	private final GetdataAsync consulta = GWT
			.create(Getdata.class);
	private ArrayList<String[]> listEstadoCivil;
	
	public Registrarse(Command co) {
		initWidget(uiBinder.createAndBindUi(this));
		comando=co;
		pass1.setEnabled(false);
		pass2.setEnabled(false);
		personales.setVisible(false);
		iniciarWidget();
		
	}
	
	public void mostrar(){
		Display.get().setWidget(this);
	}
	
	@SuppressWarnings("deprecation")
	private void iniciarWidget() {
		sexo.addItem("Masculino");
		sexo.addItem("Femenino");
		consulta.getEstadoCivil(new AsyncCallback<ArrayList<String[]>>() {
			
			@Override
			public void onSuccess(ArrayList<String[]> result) {
				listEstadoCivil=result;
				estadocivil.clear();
				for(String[] s:result){
					estadocivil.addItem(s[1]);
				}
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub				
			}
		});
		Date date=new Date(2012,6,10);
		Integer in=new Integer(date.getYear());
		for(int i=in;i>in-100;i--){
			anio.addItem((new Integer(i)).toString());
		}
		
	}
	
	public void limpiarWidget(){
		usuario.setText("");
		pass1.setText("");
		pass2.setText("");
		nombres.setText("");
		paterno.setText("");
		materno.setText("");
		direccion.setText("");
		dni.setText("");
		factor_rh.setText("");
		grupo_sanguineo.setText("");
		tb_telefono.setText("");
		
	}
	
	interface RegistrarseUiBinder extends UiBinder<Widget, Registrarse> {
	}
	
	
	@UiHandler("button")
	void onButtonClick(ClickEvent event) {
		consulta.existeUsuario(usuario.getText(), new AsyncCallback<Boolean>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(Boolean result) {
				if(result){
					pass1.setEnabled(true);
					pass2.setEnabled(true);
					personales.setVisible(true);
				}
			}
		});
		
	}

	
	@SuppressWarnings("deprecation")
	@UiHandler("anio")
	void onAnioChange(ChangeEvent event) {
		Date date=new Date(2012,6,10);
		Integer an=Integer.parseInt(anio.getValue(anio.getSelectedIndex()));
		date.setYear(an-1900);
		fecha.setValue(date);System.out.println(date.getYear());
	}
	@UiHandler("image")
	void onImageClick(ClickEvent event) {
		comando.execute();
	}
	@SuppressWarnings("deprecation")
	@UiHandler("bot_registrarse")
	void onBot_registrarseClick(ClickEvent event) {
		ArrayList<String> data=new ArrayList<String>();
		data.add(new String(listEstadoCivil.get(estadocivil.getSelectedIndex())[0]));
		data.add(dni.getText());
		data.add(nombres.getText());
		data.add(paterno.getText());
		data.add(materno.getText());
		data.add((new Integer(fecha.getValue().getDay())).toString());
		data.add((new Integer(fecha.getValue().getMonth())).toString());
		data.add((new Integer(anio.getItemText(anio.getSelectedIndex()))).toString());
		data.add(sexo.getItemText(sexo.getSelectedIndex()).substring(0,1));
		data.add(grupo_sanguineo.getText());
		data.add(factor_rh.getText());
		data.add(direccion.getText());
		data.add("correo");
		data.add("V");
		data.add(Encriptar.encriptar(pass1.getText()));
		data.add(usuario.getText());
		
		consulta.registrar(data, new AsyncCallback<Boolean>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(Boolean result) {
				// TODO Auto-generated method stub
				if(result){
					Window.alert("Registro Correctamente");
					limpiarWidget();
				}
				else Window.alert("Error registrando");
			}
		});
	}
}
